﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Customer;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service;
using Quartz;
using Customer360AutoJob.Core.Configuration;
using Customer360AutoJob.Models.ViewModels.Jobs;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading;
using WinSCP;

namespace Customer360AutoJob.Jobs
{
    public class ExportCustomerJob : IJob
    {
        #region Properties
        public UserRecord CurrentUserRecord { get; set; }

        #endregion
        public void Execute(IJobExecutionContext context)
        {
            CurrentUserRecord = new UserRecord
            {
                UserName = "admin",
                GroupIdentifier = "1"
            };
            ExportDataTotalCustomer();
            // Set up session options
           

            Thread.Sleep(100);
        }
        private bool ExportDataTotalCustomer()
        {
            var customerRecords = new RetrieveCustomerResponse();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerRequest request = new RetrieveCustomerRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Jobs.ExportCustomerJob.ExportDataTotalCustomer"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 0,
                        PageIndex = 0
                    },
                    Lang = "en",
                    CurrentUser = CurrentUserRecord,
                    Filter = new FilterRecord()
                };
                customerRecords = serviceClient.Proxy.RetrieveTotalCustomer(request);
            }
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ExportCustomerViewModel>() : customerRecords.Records.Select(x => new ExportCustomerViewModel
            {
                Cif = x.CIF,
                Cus_Name = x.Name,
                Branch_Id = x.Branch.BranchId,
                Address = x.Address,
                Cus_Open_Date = x.DateOpenCif,
                Birth_Incorp_Date = x.BirthIncorpDate,
                Legal_Id = x.LegalId,
                Vpb_Industry = x.VpbIndustry,
                Industry_Name = x.VpbIndustry,
                //Doanh_Thu=x.
                //Email_Addr=x.
                Segment = x.SubSegment,
                Dao = x.Dao,
                Cust_Type = x.CustType,
                //Cust_Type_Name= x.CustType,
                Industry_Name_En = x.VpbIndustryEn,
                Dao_Name = x.DaoName,
                Cust_Status = x.CusStatus
            }).ToList();

            DataTable dt = new DataTable();
            dt.TableName = "Table Customers ";
            var fileName = "Table Customers.xlsx";
            dt = ToDataTable(resultCustomer);

            bool IsSuccess = true;
            return IsSuccess;
        }

        #region Helper Functions
        public static DataTable ToDataTable<T>(List<T> items)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);

            //Get all the properties
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                //prop.GetDisplayName(t=>t.name);
                //Setting column names as Property names
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
                var values = new object[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {
                    //inserting property values to datatable rows
                    values[i] = Props[i].GetValue(item, null);
                }
                dataTable.Rows.Add(values);
            }
            //put a breakpoint here and check datatable
            return dataTable;
        }
        #endregion
    }
}